package com.chasmcity.munrobagging;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.chasmcity.munrobagging.map.ImprovedMapView;
import com.chasmcity.munrobagging.map.MunroOverlay;
import com.chasmcity.munrobagging.model.LatLong;
import com.chasmcity.munrobagging.model.Munro;
import com.google.android.maps.MapActivity;

public class MunroMapActivity extends MapActivity {
	
	private ImprovedMapView mapView;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.map);

        mapView = (ImprovedMapView) findViewById(R.id.mapview);
        mapView.init();
        mapView.setSatellite(true);
        
		MunroOverlay overlay = new MunroOverlay(this);
		mapView.getOverlays().add(overlay);
		mapView.setMapMovementListener(overlay);

		Munro munro = getMunro();
		if (munro != null) {
			mapView.goTo(munro.getLatLong(), getResources().getInteger(R.integer.showMunroZoomLevel), false);
		} else {
			mapView.goTo(getInitialCentrePoint(), getResources().getInteger(R.integer.initialZoomLevel), false);
		}
	}
	
	private LatLong getInitialCentrePoint() {
		return new LatLong(
				getResources().getInteger(R.integer.initialCentreLat) / 1E6,
				getResources().getInteger(R.integer.initialCentreLng) / 1E6
		);
	}
	
	private Munro getMunro() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			return (Munro) extras.getSerializable(Munro.class.getName());
		} else {
			return null;
		}
	}

	static void launch(Context context, Munro munro) {
		Intent intent = new Intent(context, MunroMapActivity.class);
		if (munro != null) {
			intent.putExtra(Munro.class.getName(), munro);
		}
		context.startActivity(intent);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}